import SolidTooltip from "@lib/Tooltip";
import { splitProps } from "solid-js";
import type { TableCellProps } from "./types";
import { getValue } from "./utils";

export const TableCell = <T = {},>(props: TableCellProps<T>) => {
    const [local, attributes] = splitProps(props, [
      "data",
      "column",
      "columnIndex",
      "rowIndex",
    ]);
  
    return local.column?.tooltip ? (
      <SolidTooltip
        as="td"
        {...attributes}
        style={local.column?.style}
        classList={local.column?.classList}
        title={
          local.column?.children?.(local.data!, local.column!, local.rowIndex) ??
          getValue(local.data!, local.column?.name!) ??
          ""
        }
      >
        {local.column?.children?.(local.data!, local.column!, local.rowIndex) ??
          getValue(local.data!, local.column?.name!) ??
          ""}
      </SolidTooltip>
    ) : (
      <td
        {...attributes}
        style={local.column?.style}
        classList={local.column?.classList}
      >
        {local.column?.children?.(local.data!, local.column, local.rowIndex) ??
          getValue(local.data!, local.column?.name!) ??
          ""}
      </td>
    );
  };
  